﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BL.Model.Request;
using Common;
using DAL.Model;

namespace BL
{
    public class ProductController
    {
        private readonly MasterSession _session;
        private readonly DocumentSeqNoHelper _documentSeqNoHelper;
        public ProductController(MasterSession session)
        {
            if (session == null)
            {
                return;
            }
            _session = session;
            _documentSeqNoHelper = new DocumentSeqNoHelper("ProductCode", _session);
        }

        public Product Create()
        {
            var product = new Product
                              {
                                  Code = _documentSeqNoHelper.GetNextDocSeqNo()
                              };
            _session.Products.Add(product);

            return product;
        }

        public List<ProductCategory> SearchProductCat(SearchingRequest request)
        {
            var isAllKeyword = string.IsNullOrWhiteSpace(request.Keyword);
            var keyword = isAllKeyword ? string.Empty : request.Keyword.Trim().ToLower();

            var query = from productCategory in _session.ProductCategories
                        where (isAllKeyword ||
                               (productCategory.Id.ToString() == keyword))
                        select productCategory;

            return query.ToList();
        }
    }
}
